#include "stdafx.h"
#include "imp_UIcompote.h"
#include "i_UIelement.h"
#include "i_UIlogic.h"
imp_UIcompte::imp_UIcompte():
m_active(false)
{

}

imp_UIcompte::~imp_UIcompte()
{
	for (LogicCollIt it=m_logics.begin();it!=m_logics.end();++it)
	{
		if (*it)
		{
			delete (*it);
			(*it)=0;
		}
	}
	m_logics.clear();

	for (ElementCollIt it=m_elements.begin();it!=m_elements.end();++it)
	{
		if ((*it).second)
		{
			delete (*it).second;
			(*it).second=0;
		}
	}
	m_elements.clear();

}

bool imp_UIcompte::add_element(std::string _name, i_UIelement* _element )
{
	ElementCollIt it=m_elements.find(_name);
	if (it==m_elements.end())
	{
		if (_element)
		{
			_element->set_compote_size(m_width*m_window_w,m_height*m_window_h);
			_element->set_compote_position(m_x*m_window_w,m_y*m_window_h);
			m_elements.insert(std::make_pair(_name,_element));
			return true;
		}
	}
	return false;

}

bool imp_UIcompte::add_logic( i_UIlogic* _logic)
{
	if (_logic)
	{
		_logic->bind_compote(this);
		m_logics.push_back(_logic);
		return true;
	}
	return false;

}

bool imp_UIcompte::remove_element( std::string _name )
{
	ElementCollIt it=m_elements.find(_name);
	if (it!=m_elements.end())
	{
		if ((*it).second)
		{
			delete (*it).second;
			(*it).second=0;
		}
		m_elements.erase(it);
		return true;

	}
	return false;

}

void imp_UIcompte::set_windos_size( int _w,int _h )
{
	m_window_w=_w;
	m_window_h=_h;
	for (ElementCollIt it=m_elements.begin();it!=m_elements.end();++it)
	{
		if ((*it).second)
		{
		   (*it).second->set_compote_position(m_x*m_window_w,m_y*m_window_h);
		   (*it).second->set_compote_size(m_width*m_window_w,m_height*m_window_h);

		}
	}

}

void imp_UIcompte::on_message( ILTMessage_Read* _msg)
{
	for (LogicCollIt it=m_logics.begin();it!=m_logics.end();++it)
	{
		(*it)->on_message(_msg);
	}

}